<template>
  <ContentWrap>
    <el-button plain @click="resetQuery"> 重置 </el-button>
    <el-button
      v-hasPermi="['erp:print-document:create']"
      type="primary"
      plain
      @click="push({ name: 'ErpCustom', query: { id: null } })"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" border>
      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="单据类型" align="center" prop="documentType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ERP_DOCUMENT" :value="scope.row.documentType" />
        </template>
      </el-table-column>
      <el-table-column label="默认状态" align="center" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            active-text="默认"
            inactive-text="不默认"
            @change="change(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            v-hasPermi="['erp:print-document:update']"
            link
            type="primary"
            @click="push({ name: 'ErpCustom', query: { id: scope.row.id } })"
          >
            编辑
          </el-button>
          <el-button
            v-hasPermi="['erp:print-document:delete']"
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
// import download from '@/utils/download'
import { PrintDocumentApi, PrintDocumentVO } from '@/api/erp/printdocument'
const { push } = useRouter()
/** 打印模板 列表 */
defineOptions({ name: 'PrintDocument' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PrintDocumentVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  documentType: undefined,
  status: undefined
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PrintDocumentApi.getPrintDocumentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  handleQuery()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PrintDocumentApi.deletePrintDocument(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const change = async (row) => {
  await PrintDocumentApi.updatePrintDocument(row)
  message.success('修改成功')
  resetQuery()
}

// 首次进入或从缓存中激活时调用
onActivated(() => {
  console.log('PreviousPage activated')
  getList()
})

/** 初始化 **/
onMounted(() => {
  // getList()
})
</script>
